package edu.bit.water.service;

import edu.bit.water.dao.EntitySensorDao;
import edu.bit.water.dao.WarningRuleDao;
import edu.bit.water.dto.WarningRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: WarningRule的service层
 * @Author: YYF
 */
@Service
@Component
public class WarningRuleService {
    @Autowired
    private WarningRuleDao warningRuleDao;

    public List<WarningRule> queryWarningRule() {
        return warningRuleDao.queryWarningRule();
    }

    public List<WarningRule> queryWarningRuleByCompanyId(String company_id) {
        return warningRuleDao.queryWarningRuleByCompanyId(company_id);
    }

    public void deleteWarningRule(String idList) {
        warningRuleDao.deleteWarningRule(idList);
    }

    public Map<String, Object> insertWarningRule(WarningRule warningRule) {
        Map<String, Object> modelMap = new HashMap<>();
        boolean flag = true;
        String[] sensors = warningRule.getSensorRowId().split(",");
        Integer id = warningRuleDao.getMaxId();
        if (id == null) {
            id = -1;
        }
        for (int i = 0; i < sensors.length; i++) {//检测所有的传感器规则是否已经存在
            warningRule.setSensorRowId(sensors[i]);
            if (warningRuleDao.querySensorRowIdExist(warningRule.getSensorRowId(), warningRule.getCompanyId()) > 0) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "该传感器规则已存在，插入失败");
                modelMap.put("sensorRowId", sensors[i]);
                flag = false;//存在无法插入的传感器则拒绝插入整条规则
                continue;
            }
        }
        if(flag) {
            for (int i = 0; i < sensors.length; i++) {//逐个插入传感器
                warningRule.setSensorRowId(sensors[i]);
                id = id + 1;
                //warningRule.setId(id);
                warningRuleDao.insertWarningRule(warningRule);
            }
            modelMap.put("status", "success");
            return modelMap;
        }
        else {
            return modelMap;
        }
        /*
        if (warningRuleDao.querySensorRowIdExist(warningRule.getSensorRowId()) > 0) {
            modelMap.put("status", "failed");
            modelMap.put("msg", "该传感器规则已存在，插入失败");
            modelMap.put("sensorRowId", warningRule.getSensorRowId());
            return modelMap;
        }
        else {
            warningRuleDao.insertWarningRule(warningRule);
            modelMap.put("status", "success");
            return modelMap;
        }
        */
    }

    public Map<String, Object> updateWarningRule(WarningRule warningRule) {
        Map<String, Object> modelMap = new HashMap<>();
//        if (warningRule.getSensorRowId()!=null&&warningRuleDao.querySensorRowIdExist(warningRule.getSensorRowId()) > 0) {
//            modelMap.put("status", "failed");
//            modelMap.put("msg", "该传感器规则已存在,更新失败");
//            return modelMap;
//        }
        warningRuleDao.updateWarningRule(warningRule);
        modelMap.put("status", "success");
        return modelMap;
    }

    public Map<String, Object> queryRuleByNameAndCreateDate(String name, String createDate) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<WarningRule> result = warningRuleDao.queryRuleByNameAndCreateDate(name, createDate);
        List<WarningRule> result_ = new ArrayList<WarningRule>();
        for (int i = 0; i < result.size(); i++) {
            String wname = result.get(i).getName();
            String wcreateDate = result.get(i).getCreateDate();
            if (name.contains(wname) && createDate.contains(wcreateDate)) {
                result_.add(result.get(i));
            }
        }
        map.put("status", "success");
        map.put("result", result_);
        return map;
    }


    /**
     * 根据所在位置查询设备类型
     */
    public Map<String, Object> querySensorTypeByLocation(String businessId, String locations){
        Map<String, Object> map = new HashMap<>();
        map.put("status", "success");
        if(locations.startsWith("[")) {
            locations = locations.replace("[", "");
        }
        if(locations.endsWith("]")) {
            locations = locations.replace("]", "");
        }
        locations = locations.replace("\"", "");
        String[] list = locations.split(",");
        List<String> lis = new ArrayList<>();
        for (String s : list) {
            lis.add(s);
        }
        map.put("list", warningRuleDao.querySensorTypeByLocation(businessId, lis));
        return map;
    }
}
